package com.daiqee.service.impl;

import java.io.File;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.daiqee.pojo.ProcurementContract;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.daiqee.dao.mapper.ProcurementContractMapper;

/**
 * 采购合同
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月8日 上午8:24:29
 * @package com.daiqee.service.impl
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Service
public class ProcurementConractService extends ServiceImpl<ProcurementContractMapper, ProcurementContract> {

	@Value("${pic.contract}")
	private String contractImgSavePath;
	
	@Autowired
	private ProcurementContractMapper mapper;

	/**
	 * 数据存入硬盘中并且 保存到数据库
	 */
	public Boolean saveContractImg(MultipartFile contractImages, String id) throws IllegalStateException, IOException {
		String oldFileName = contractImages.getOriginalFilename(); // 获取上传文件的原名
		if(oldFileName != null && oldFileName.length() > 0) {
			// 新的图片名称
	        String newFileName = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SS").format(new Date())
	        		+ "-" + oldFileName;
	        // 新图片
	        File newFile = new File(contractImgSavePath + "\\" + newFileName);
	        // 将内存中的数据写入磁盘
	        contractImages.transferTo(newFile);
	        ProcurementContract currentContract = mapper.selectById(id);
	        if(currentContract.getProcureConBrowse() == null) {
	        	currentContract.setProcureConBrowse("");
	        }
	        currentContract.setProcureConBrowse(currentContract.getProcureConBrowse() + "|" + newFileName);
	        mapper.updateById(currentContract);
		}else {
			return false;
		}
		return true;
	}
}
